package org.polyforms.repository.converter;

import java.util.Collections;
import java.util.Set;

import javax.inject.Inject;

import org.polyforms.repository.spi.EntityHelper;
import org.polyforms.repository.spi.IdentifierHelper;
import org.springframework.core.convert.converter.ConditionalGenericConverter;

/**
 * Abstract class of converter for entity.
 * 
 * @author Kuisong Tong
 * @since 1.0
 */
abstract class EntityConverter implements ConditionalGenericConverter {
    @Inject
    private EntityHelper entityHelper;
    @Inject
    private IdentifierHelper identifierHelper;

    /**
     * {@inheritDoc}
     */
    public Set<ConvertiblePair> getConvertibleTypes() {
        return Collections.singleton(new ConvertiblePair(Object.class, Object.class));
    }

    protected boolean canBeConverted(final Class<?> entityClass, final Class<?> identifierClass) {
        return entityHelper.isEntity(entityClass)
                && identifierHelper.getIdentifierClass(entityClass) == identifierClass;
    }

    protected IdentifierHelper getIdentifierHelper() {
        return identifierHelper;
    }
}
